﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MT.CustomUserControl.ViewModels
{
    public class LoginUcViewModel : ObservableObject
    {
        /// <summary>
        /// 软件名称
        /// </summary>
        private string softwareName = "智能中医脉诊系统";
        public string SoftwareName
        {
            get { return softwareName; }
            set { SetProperty(ref softwareName, value); }
        }

        /// <summary>
        /// 错误信息
        /// </summary>
        private string m_ErrorMessage;
        public string ErrorMessage
        {
            get { return m_ErrorMessage; }
            set { SetProperty(ref m_ErrorMessage, value); }
        }

        /// <summary>
        /// 记录登录信息
        /// </summary>
        private bool m_SaveUserInfo = false;
        public bool SaveUserInfo
        {
            get { return m_SaveUserInfo; }
            set { SetProperty(ref m_SaveUserInfo, value); }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        private string userName = string.Empty;
        public string UserName
        {
            get { return userName; }
            set { SetProperty(ref userName, value); }
        }

        /// <summary>
        /// 密码
        /// </summary>
        private string userPassword = string.Empty;
        public string UserPassword
        {
            get { return userPassword; }
            set { SetProperty(ref userPassword, value); }
        }

        /// <summary>
        /// 记录登录成功的历史名称
        /// </summary>
        private ObservableCollection<string> m_UserHistory = new ObservableCollection<string>();
        public ObservableCollection<string> UserHistory
        {
            get { return m_UserHistory; }
            set { SetProperty(ref m_UserHistory, value); }
        }

        /// <summary>
        /// 退出登录页面的Action
        /// </summary>
        public Action LoginExitAction;

        /// <summary>
        /// 登录退出
        /// </summary>
        private RelayCommand m_LoginExitCommand;
        public RelayCommand LoginExitCommand
        {
            get
            {
                if (m_LoginExitCommand == null)
                {
                    m_LoginExitCommand = new RelayCommand(() =>
                    {
                        LoginExitAction?.Invoke();
                    });
                }
                return m_LoginExitCommand;
            }
        }

        /// <summary>
        /// 登录Action，传递用户名和密码
        /// </summary>
        public Action<string, string> LoginAction;

        /// <summary>
        /// 登录
        /// </summary>
        private RelayCommand m_LoginCommand;
        public RelayCommand LoginCommand
        {
            get
            {
                if (m_LoginCommand == null)
                {
                    m_LoginCommand = new RelayCommand(LoginCommandFunc);
                }
                return m_LoginCommand;
            }
        }

        public LoginUcViewModel()
        {

        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="obj">密码框的父级窗体</param>
        private void LoginCommandFunc()
        {
            LoginAction?.Invoke(UserName, UserPassword);
        }
    }
}
